package com.jf.blog.controller;

import com.jf.blog.bean.Userinfo;
import com.jf.blog.form.vip.VipAuthForm;
import com.jf.blog.form.vip.VipPasswordForm;
import com.jf.blog.form.vip.VipSearchForm;
import com.jf.blog.form.vip.VipUpdateForm;
import com.jf.blog.service.UserinfoService;
import com.jf.blog.util.MyConstants;
import com.jf.blog.util.ResultJson;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiOperation;
import io.swagger.annotations.ApiParam;
import org.springframework.beans.BeanUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;

import java.util.List;

@RestController
@RequestMapping("/vip")
@Api(tags = "会员模块")
public class VipController {
    @Autowired
    private UserinfoService userinfoService;

    @PostMapping("/auth")
    @ApiOperation("会员认证")
    public ResultJson auth(@RequestBody VipAuthForm form) {
        userinfoService.vipAuth(form);
        return ResultJson.success();
    }

    @PostMapping("/update")
    @ApiOperation("修改信息")
    public ResultJson update(@RequestBody VipUpdateForm form) {
        userinfoService.vipUpdate(form);
        return ResultJson.success();
    }

    @PostMapping("/password")
    @ApiOperation("修改密码")
    public ResultJson password(@RequestBody VipPasswordForm form) {
        userinfoService.vipPassword(form);
        return ResultJson.success();
    }

    @GetMapping("/page")
    @ApiOperation("分页筛选")
    public ResultJson page(@RequestParam(defaultValue = "1") @ApiParam(required = true) int page, @RequestParam(defaultValue = "5") @ApiParam(required = true) int limit, VipSearchForm form) {
        Userinfo userinfo = new Userinfo();
        BeanUtils.copyProperties(form, userinfo);
        userinfo.setRoleId(MyConstants.ROLE_VIP);
        return userinfoService.getPage(page, limit, userinfo);
    }

    @DeleteMapping("/delete")
    @ApiOperation("批量删除")
    public ResultJson delete(@RequestBody List<Integer> userinfoIds) {
        userinfoService.deleteVip(userinfoIds);
        return ResultJson.success();
    }

    @GetMapping("/info/{vipId}")
    @ApiOperation("会员详情")
    public ResultJson info(@PathVariable int vipId) {
        return ResultJson.success(userinfoService.getById(vipId));
    }

    @PostMapping("/status/{vipId}")
    @ApiOperation("修改状态")
    public ResultJson status(@PathVariable int vipId) {
        userinfoService.updateStatus(vipId);
        return ResultJson.success();
    }
}
